<template>
  <div class="login-container">
    <el-row justify="center" align="middle" style="height: 100%;">
      <el-col :span="12" :sm="16" :xs="22">
        <div class="login-box">
          <h2 class="box-title">WebSSH 管理系统</h2>
          <el-form>
            <div class="login-form">
              <el-form-item>
                <el-input v-model="data.name" trim minlength="1" maxlength="64" show-word-limit clearable
                  placeholder="请输入用户名" class="login-input" size="large">
                  <template #prefix><el-icon class="input-icon">
                      <User />
                    </el-icon></template>
                </el-input>
              </el-form-item>

              <el-form-item>
                <el-input v-model="data.pwd" trim type="password" minlength="3" maxlength="64" show-word-limit
                  show-password clearable placeholder="请输入密码" class="login-input" size="large">
                  <template #prefix><el-icon class="input-icon">
                      <Lock />
                    </el-icon></template>
                </el-input>
              </el-form-item>

              <el-form-item>
                <el-button @click="login" type="primary" class="login-button" size="large">
                  登录系统
                </el-button>
              </el-form-item>
            </div>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import axios from "axios";
import { useGlobalStore } from "@/stores/store";
import { User, Lock } from '@element-plus/icons-vue'

let router = useRouter();
let globalStore = useGlobalStore();

let data = reactive({
  name: "",
  pwd: "",
})

interface ResponseData {
  code: number;
  msg: string;
  token: string;
  is_admin: "Y" | "N";
  is_root: "Y" | "N";
  user_name: string;
  user_desc: string;
  user_expiry_at: string;
}

/**
 * 登陆系统
 */
function login() {
  if (data.name.trim().length < 2) {
    ElMessage.error("用户名至少两个字符")
    return
  }

  if (data.pwd.trim().length < 2) {
    ElMessage.error("密码至少两个字符")
    return
  }

  axios.post<ResponseData>("/api/login", data).then((ret) => {
    if (ret.data.code === 0) {
      ElMessage.success("登陆成功");
      localStorage.setItem("token", ret.data.token);
      localStorage.setItem("auth", "yes");
      let res = ret.data;
      globalStore.login(res.is_admin, res.is_root, res.user_name, res.user_desc, res.user_expiry_at);
      router.push({ name: "Home" });
    } else {
      ElMessage.error("登陆失败");
    }
  }).catch(() => {
    ElMessage.error("登陆失败");
  })
}

</script>

<style scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(135deg, #1e88e5 0%, #64b5f6 100%);
  display: flex;
  align-items: center;
}

.box-title {
  color: #1976d2;
  text-align: center;
  font-size: 2em;
  margin-bottom: 2.5rem;
  font-weight: 500;
}

.login-box {
  background: rgba(255, 255, 255, 0.98);
  border-radius: 15px;
  padding: 3.5rem 4rem;
  box-shadow: 0 8px 32px rgba(25, 118, 210, 0.15);
  backdrop-filter: blur(10px);
  animation: fadeIn 0.5s ease-out;
  min-width: 400px;
}

.login-form {
  padding: 0.5rem 1rem;
}

.login-input {
  --el-input-height: 45px;
}

.input-icon {
  font-size: 1.2em;
  margin-right: 0.5rem;
  color: #1976d2;
}

.login-input :deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(25, 118, 210, 0.08);
  padding: 0.5rem 1rem;
}

.login-input :deep(.el-input__wrapper):hover {
  box-shadow: 0 2px 12px 0 rgba(25, 118, 210, 0.15);
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(45deg, #1976d2, #64b5f6);
  border: none;
  transition: all 0.3s ease;
  margin-top: 1rem;
  color: white;
  font-weight: 500;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(25, 118, 210, 0.3);
  background: linear-gradient(45deg, #1565c0, #42a5f5);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .login-box {
    margin: 1rem;
    padding: 2.5rem;
    min-width: auto;
  }

  .box-title {
    font-size: 1.8em;
    margin-bottom: 2rem;
  }
}
</style>
